<?php


namespace App\Models;


class Coupon extends BaseModel
{
    protected $table = 'coupons';

    protected $fillable = [
        'user_id',
        'forwarder_id',
        'title',
        'name',
        'end_time',
        'order_num',
        'order_amount',
        'order_comment',
        'order_amount_unit',
        'award_type',
        'pea',
        'pea_max',
        'discount',
        'discount_max',
    ];

    public function setTitleAttribute()
    {
        $title = '';
        if ($this->order_num > 0) {
            $title = $title . '完成' . $this->order_num . '张订单';
        }
        if ($this->order_amount > 0) {
            $title = $title . '消费满' . $this->order_amount_unit . $this->order_amount;
        }
        if ($this->order_comment > 0) {
            $title = $title . '好评';
        }
        if ($this->award_type === 0) {
            $title = $title . '返' . $this->pea . '智慧豆';
        } else if ($this->award_type === 1) {
            $title = $title . '返' . $this->discount . '折优惠券';
        }
        $this->attributes['title'] = $title;
    }

    public function coupon_user()
    {
        return $this->hasOne('App\Models\CouponUser', 'coupon_id', 'id');
    }

    public static function getTitle($obj)
    {
        $title = '';
        if ($obj->order_num > 0) {
            $title = $title . '完成' . $obj->order_num . '张订单';
        }
        if ($obj->order_amount > 0) {
            $title = $title . '消费满' . $obj->order_amount_unit . $obj->order_amount;
        }
        if ($obj->order_comment > 0) {
            $title = $title . '好评';
        }
        if ($obj->award_type === 0) {
            $title = $title . '返' . $obj->pea . '智慧豆';
        } else if ($obj->award_type === 1) {
            $title = $title . '返' . $obj->discount . '折优惠券';
        }
       return $title;
    }
}
